import { storageUtils } from '../../utils/storageUtils'
import { reqLogin } from '../../api'
// 绑定表单监听事件
document.getElementById('loginForm').addEventListener('submit', async function (e) {
    e.preventDefault() // 阻止默认行为
    // 获取输入的用户名和密码
    const username = document.getElementById('username').value
    const pwd = document.getElementById('pwd').value
    // 发送请求
    const result = await reqLogin(username, pwd)
    // 登录失败的提示信息
    if (result.code !== 200) {
        alert(result.msg)
    } else {
        // 登录成功，保存token和user
        storageUtils.setToken(result.data.token)
        storageUtils.setUser(result.data.user)
        // 跳转到管理页面
        window.location.href = '/users.html'
    }

    // 清空输入框
    document.getElementById('username').value = ''
    document.getElementById('pwd').value = ''
})
